package com.tduck.cloud.api.web.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tduck.cloud.api.annotation.Log;
import com.tduck.cloud.api.annotation.Login;
import com.tduck.cloud.common.util.CacheUtils;
import com.tduck.cloud.common.util.Result;
import com.tduck.cloud.form.entity.UserFormDataEntity;
import com.tduck.cloud.form.service.FormDashboardService;
import com.tduck.cloud.form.service.UserFormDataService;
import com.tduck.cloud.form.util.JsonContainsUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.tduck.cloud.form.constant.FormConstants.FORM_VIEW_COUNT;

/**
 * @author : smalljop
 * @description : 报表相关接口
 * @create :  2020/12/30 16:47
 **/

@RestController
@RequiredArgsConstructor
public class FormDashboardController {

    private final UserFormDataService userFormDataService;
    private final FormDashboardService formDashboardService;
    private final CacheUtils cacheUtils;


    /**
     * 表单收集信息
     */
    @Login
    @Log(operation = "表单收集信息")
    @GetMapping("/user/form/report/stats")
    public Result formReportStats(String formKey, String queryData, String beginDate, String endDate) {
        //浏览量
        String viewCount = cacheUtils.get(StrUtil.format(FORM_VIEW_COUNT, formKey));
        //平均完成时间
        Map<String, Object> resultMap = new HashMap<>();
        List<UserFormDataEntity> list = userFormDataService.list(Wrappers.<UserFormDataEntity>lambdaQuery().eq(UserFormDataEntity::getFormKey, formKey)
                .eq(UserFormDataEntity::getDeleteFlag, false)
                .isNotNull(UserFormDataEntity::getOriginalData)
                .ge(UserFormDataEntity::getTime, beginDate)
                .lt(UserFormDataEntity::getTime, endDate));
        List<UserFormDataEntity> jsonContainsAll = JsonContainsUtils.getJsonContainsAll(queryData, list);
        long count = jsonContainsAll.size();
        resultMap.put("viewCount", viewCount);
        resultMap.put("avgCompleteTime", 0);
        resultMap.put("completeCount", count);
        return Result.success(resultMap);
    }


    /**
     * 表单收集情况 按周查看
     */
    @Login
    @Log(operation = "表单收集情况 按周查看")
    @GetMapping("/user/form/report/situation")
    public Result formReportSituation(String formKey, String queryData, String beginDate, String endDate) {
        return Result.success(formDashboardService.formReportSituation(formKey, queryData, beginDate, endDate));
    }


    /**
     * 项目收集位置情况
     */
    @Login
    @Log(operation = "项目收集位置情况")
    @GetMapping("/user/form/report/position")
    public Result formReportPosition(String formKey, String queryData, String beginDate, String endDate) {
        return Result.success(formDashboardService.formReportPosition(formKey, queryData, beginDate, endDate));
    }


    /**
     * 项目收集设备
     */
    @Login
    @Log(operation = "项目收集设备")
    @GetMapping("/user/form/report/device")
    public Result formReportDevice(String formKey, String queryData, String beginDate, String endDate) {
        return Result.success(formDashboardService.formReportDevice(formKey, queryData, beginDate, endDate));
    }


    /**
     * 项目收集来源
     */
    @Login
    @Log(operation = "项目收集来源")
    @GetMapping("/user/form/report/source")
    public Result formReportSource(String formKey, String queryData, String beginDate, String endDate) {
        return Result.success(formDashboardService.formReportSource(formKey, queryData, beginDate, endDate));
    }

    /**
     * 数据分析
     */
    @Login
    @Log(operation = "数据分析")
    @GetMapping("/user/form/report/analysis")
    public Result formReportAnalysis(String formKey, String queryData, String beginDate, String endDate) {
        return Result.success(formDashboardService.formReportAnalysis(formKey, queryData, beginDate, endDate));
    }


    /**
     * 临床科室满意度统计
     */
    @Login
    @Log(operation = "临床科室满意度统计")
    @GetMapping("/user/form/report/satisfaction")
    public Result formReportSatisfaction(String formKey, String queryData, String beginDate, String endDate) {
        return Result.success(formDashboardService.formReportSatisfaction(formKey, queryData, beginDate, endDate));
    }

    /**
     * 临床科室满意度统计
     */
    // @Login
    @Log(operation = "满意度统计")
    @GetMapping("/user/form/report/medicalSkill")
    public Result formReportMedicalSkill(String type, String formKey, String queryData, String beginDate, String endDate) {
        return Result.success(formDashboardService.formReportMedicalSkill(type, formKey, queryData, beginDate, endDate));
    }

}
